package com.huawei.hms.support.api.pay;

import android.content.Intent;
import android.os.Bundle;
import com.huawei.hms.api.HuaweiApiAvailability;
import com.huawei.hms.api.HuaweiApiClient;
import com.huawei.hms.core.aidl.IMessageEntity;
import com.huawei.hms.support.api.PendingResultImpl;
import com.huawei.hms.support.api.client.ApiClient;
import com.huawei.hms.support.api.client.PendingResult;
import com.huawei.hms.support.api.client.Result;
import com.huawei.hms.support.api.client.Status;
import com.huawei.hms.support.api.entity.pay.GamePayReq;
import com.huawei.hms.support.api.entity.pay.GameProductPayReq;
import com.huawei.hms.support.api.entity.pay.HwPayConstant;
import com.huawei.hms.support.api.entity.pay.HwWalletInfoRequest;
import com.huawei.hms.support.api.entity.pay.InternalPayRequest;
import com.huawei.hms.support.api.entity.pay.OrderRequest;
import com.huawei.hms.support.api.entity.pay.PayNaming;
import com.huawei.hms.support.api.entity.pay.PayReq;
import com.huawei.hms.support.api.entity.pay.PayResp;
import com.huawei.hms.support.api.entity.pay.ProductDetailRequest;
import com.huawei.hms.support.api.entity.pay.ProductPayRequest;
import com.huawei.hms.support.api.entity.pay.PurchaseInfoInnerRequest;
import com.huawei.hms.support.api.entity.pay.PurchaseInfoRequest;
import com.huawei.hms.support.api.entity.pay.WalletUiIntentReq;
import com.huawei.hms.support.api.entity.pay.WithholdRequest;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant.KeyAndValue;
import com.huawei.hms.support.hianalytics.HiAnalyticsUtil;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.utils.Util;

public class HuaweiPayApiImpl implements HuaweiPayApi {

    static class f extends PendingResultImpl<PayResult, PayResp> {
        public final /* synthetic */ Result onComplete(IMessageEntity iMessageEntity) {
            PayResp payResp = (PayResp) iMessageEntity;
            if (payResp == null) {
                HMSLog.e("HuaweiPayApiImpl", "pay resp is null");
                return null;
            }
            HMSLog.i("HuaweiPayApiImpl", "pay resp :" + payResp.retCode);
            Result payResult = new PayResult();
            payResult.setStatus(new Status(payResp.retCode, null, payResp.getPendingIntent()));
            return payResult;
        }

        public f(ApiClient apiClient, String str, IMessageEntity iMessageEntity) {
            super(apiClient, str, iMessageEntity);
        }
    }

    public PendingResult<PayResult> pay(HuaweiApiClient huaweiApiClient, PayReq payReq) {
        HMSLog.i("HuaweiPayApiImpl", "Enter pay");
        HiAnalyticsUtil.getInstance().onEvent(huaweiApiClient.getContext().getApplicationContext(), KeyAndValue.START_PAY, payReq.requestId);
        return new f(huaweiApiClient, PayNaming.pay, new GamePayReq(payReq, huaweiApiClient.getCpID()));
    }

    public PendingResult<PayResult> addWithholdingPlan(HuaweiApiClient huaweiApiClient, WithholdRequest withholdRequest) {
        HMSLog.i("HuaweiPayApiImpl", "Enter addWithholdingPlan");
        if (Util.getHmsVersion(huaweiApiClient.getContext()) < 20504000) {
            return new e();
        }
        return new f(huaweiApiClient, PayNaming.withhold, withholdRequest);
    }

    public PendingResult<OrderResult> getOrderDetail(HuaweiApiClient huaweiApiClient, OrderRequest orderRequest) {
        HMSLog.i("HuaweiPayApiImpl", "Enter getOrderDetail");
        return new a(huaweiApiClient, PayNaming.getOrderDetail, orderRequest);
    }

    public PendingResult<PurchaseInfoResult> getPurchaseInfo(HuaweiApiClient huaweiApiClient, PurchaseInfoRequest purchaseInfoRequest) {
        HMSLog.i("HuaweiPayApiImpl", "Enter getPurchaseInfo");
        if (Util.getHmsVersion(huaweiApiClient.getContext()) < HuaweiApiAvailability.HMS_VERSION_CODE_OD) {
            return new b();
        }
        return new c(huaweiApiClient, PayNaming.purchaseinfo, new PurchaseInfoInnerRequest(purchaseInfoRequest));
    }

    public PayResultInfo getPayResultInfoFromIntent(Intent intent) {
        if (intent == null) {
            HMSLog.e("HuaweiPayApiImpl", "getPayResultInfoFromIntent intent is null");
            return null;
        }
        Bundle extras = intent.getExtras();
        if (extras == null) {
            HMSLog.e("HuaweiPayApiImpl", "getPayResultInfoFromIntent bundle is null");
            return null;
        }
        PayResultInfo payResultInfo = new PayResultInfo();
        payResultInfo.setReturnCode(extras.getInt("returnCode"));
        payResultInfo.setUserName(extras.getString(HwPayConstant.KEY_USER_NAME));
        payResultInfo.setOrderID(extras.getString("orderID"));
        payResultInfo.setAmount(extras.getString(HwPayConstant.KEY_AMOUNT));
        payResultInfo.setCountry(extras.getString(HwPayConstant.KEY_COUNTRY));
        payResultInfo.setCurrency(extras.getString(HwPayConstant.KEY_CURRENCY));
        payResultInfo.setWithholdID(extras.getString("withholdID"));
        payResultInfo.setRequestId(extras.getString(HwPayConstant.KEY_REQUESTID));
        payResultInfo.setErrMsg(extras.getString("errMsg"));
        payResultInfo.setTime(extras.getString("time"));
        payResultInfo.setSign(extras.getString(HwPayConstant.KEY_SIGN));
        HMSLog.i("HuaweiPayApiImpl", "final pay result info::" + payResultInfo.getReturnCode());
        return payResultInfo;
    }

    public PendingResult<ProductDetailResult> getProductDetails(HuaweiApiClient huaweiApiClient, ProductDetailRequest productDetailRequest) {
        HMSLog.i("HuaweiPayApiImpl", "Enter getProductDetails");
        return new g(huaweiApiClient, PayNaming.productdetail, productDetailRequest);
    }

    public PendingResult<PayResult> productPay(HuaweiApiClient huaweiApiClient, ProductPayRequest productPayRequest) {
        HMSLog.i("HuaweiPayApiImpl", "Enter productPay");
        HiAnalyticsUtil.getInstance().onEvent(huaweiApiClient.getContext().getApplicationContext(), KeyAndValue.START_PAY, productPayRequest.requestId);
        return new f(huaweiApiClient, PayNaming.pms, new GameProductPayReq(productPayRequest, huaweiApiClient.getCpID()));
    }

    public ProductPayResultInfo getProductPayResultFromIntent(Intent intent) {
        if (intent == null) {
            HMSLog.e("HuaweiPayApiImpl", "getProductPayResultFromIntent intent is null");
            return null;
        }
        Bundle extras = intent.getExtras();
        if (extras == null) {
            HMSLog.e("HuaweiPayApiImpl", "getProductPayResultFromIntent bundle is null");
            return null;
        }
        ProductPayResultInfo productPayResultInfo = new ProductPayResultInfo();
        productPayResultInfo.setReturnCode(extras.getInt("returnCode"));
        productPayResultInfo.setOrderID(extras.getString("orderID"));
        productPayResultInfo.setErrMsg(extras.getString("errMsg"));
        productPayResultInfo.setProductNo(extras.getString(HwPayConstant.KEY_PRODUCT_NO));
        productPayResultInfo.setMicrosAmount(extras.getLong("microsAmount"));
        productPayResultInfo.setCurrency(extras.getString(HwPayConstant.KEY_CURRENCY));
        productPayResultInfo.setRequestId(extras.getString(HwPayConstant.KEY_REQUESTID));
        productPayResultInfo.setMerchantId(extras.getString(HwPayConstant.KEY_MERCHANTID));
        productPayResultInfo.setTime(extras.getString("time"));
        productPayResultInfo.setCountry(extras.getString(HwPayConstant.KEY_COUNTRY));
        productPayResultInfo.setSign(extras.getString(HwPayConstant.KEY_SIGN));
        HMSLog.i("HuaweiPayApiImpl", "final product pay result info::" + productPayResultInfo.getReturnCode());
        return productPayResultInfo;
    }

    public PendingResult<PayResult> internalPay(HuaweiApiClient huaweiApiClient, InternalPayRequest internalPayRequest) {
        HMSLog.i("HuaweiPayApiImpl", "Enter internalPay");
        if (Util.getHmsVersion(huaweiApiClient.getContext()) < HuaweiApiAvailability.HMS_VERSION_CODE_OD) {
            return new e();
        }
        return new f(huaweiApiClient, PayNaming.internalPay, internalPayRequest);
    }

    public PendingResult<HwWalletInfoResult> queryWalletInfo(HuaweiApiClient huaweiApiClient, HwWalletInfoRequest hwWalletInfoRequest) {
        HMSLog.i("HuaweiPayApiImpl", "Enter queryWalletInfo");
        if (Util.getHmsVersion(huaweiApiClient.getContext()) < 20602000) {
            return new h();
        }
        return new i(huaweiApiClient, PayNaming.walletQuery, hwWalletInfoRequest);
    }

    public PendingResult<GetWalletUiIntentResult> getWalletUiIntent(HuaweiApiClient huaweiApiClient, int i) {
        HMSLog.i("HuaweiPayApiImpl", "Enter getWalletUiIntent type:".concat(String.valueOf(i)));
        if (Util.getHmsVersion(huaweiApiClient.getContext()) < 20602000) {
            return new d();
        }
        IMessageEntity walletUiIntentReq = new WalletUiIntentReq();
        walletUiIntentReq.setType(i);
        return new 1(huaweiApiClient, PayNaming.getwalletintent, walletUiIntentReq);
    }
}
